﻿using lDal;

namespace DAL
{
    public class CaseDal<T>:lCaseDal<T> where T : class
    {
        public MyDbContext Db { get; }

        public CaseDal(MyDbContext db)
        {
            Db = db;
        }

        /// <summary>
        /// 单个添加
        /// </summary>
        /// <param name="T">定义名称</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> AddAsync(T T)
        {
            Db.Set<T>().AddAsync(T);
            return Db.SaveChangesAsync();
        }

        /// <summary>
        /// 多个添加
        /// </summary>
        /// <param name="list">定义表名</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> AddRangeAsync(List<T> list)
        {
            Db.Set<T>().AddRangeAsync(list);
            return Db.SaveChangesAsync();
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="T">定义名称</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> RemoveAsync(T T)
        {
            Db.Set<T>().Remove(T);
            return Db.SaveChangesAsync();
        }

        /// <summary>
        /// 多个删除
        /// </summary>
        /// <param name="list">定义表名</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> RemoveRangeAsync(List<T> list)
        {
            Db.Set<T>().RemoveRange(list);
            return Db.SaveChangesAsync();
        }


        /// <summary>
        /// 单个修改
        /// </summary>
        /// <param name="T">定义名称</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> UpdateAsync(T T)
        {
            Db.Set<T>().Update(T);
            return Db.SaveChangesAsync();
        }

        /// <summary>
        /// 多个修改
        /// </summary>
        /// <param name="list">定义表名</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> UpdateRangeAsync(List<T> list)
        {
            Db.Set<T>().UpdateRange(list);
            return Db.SaveChangesAsync();
        }


        /// <summary>
        /// 显示数据
        /// </summary>
        /// <returns>查询显示</returns>
        public IQueryable<T> GetList()
        {
            return Db.Set<T>();
        }
    }
}
